import { MdDialogRef, MdDialogConfig, MdDialog, MdSnackBar, MdSnackBarConfig } from '@angular/material';
import { Confirm, ConfirmParams } from './primeng/confirm/confirm.module';
import { Message } from "./primeng/common";
export class MessageBoxService {
    // private container: IMessageBoxContainer
    private confirm: Confirm;
    static Current = new MessageBoxService();
    dialogRef: MdDialogRef<Confirm>;
    snackBar: MdSnackBar;
    config: MdDialogConfig = {
        disableClose: false,
        width: "200px"
    }
    Regist(confirm: Confirm) {
        if (confirm) {
            this.confirm = confirm;
        }
    }

    Show(dialog: MdDialog, params: ConfirmParams) {
        this.dialogRef = dialog.open(Confirm, this.config);
        if (params.title) {
            this.confirm.title = params.title;
        }
        this.confirm.content = params.content;
        if (params.showOK != undefined) {
            this.confirm.showOK = params.showOK;
        }
        if (params.showCancel != undefined) {
            this.confirm.showCancel = params.showCancel;
        }
        if (params.showIgnore != undefined) {
            this.confirm.showIgnore = params.showIgnore;
        }
        this.confirm.confirmText = params.confirmText;
        this.confirm.cancletext = params.cancelText;
        this.confirm.ignoreText = params.ignoreText;

        return new Promise<MessageResult>((resolve, reject) => {
            this.dialogRef.afterClosed().subscribe(result => {
                let r: MessageResult;
                switch (result) {
                    case "ok": r = MessageResult.Ok; break;
                    case "cancel": r = MessageResult.Cancel; break;
                    case "ignore": r = MessageResult.Ignore; break;
                    default: break;
                }
                resolve(r);
            });
        });
    }

    Growl(type: GrowlMessageType, snackBar: MdSnackBar, content: string, selfConfig?: MdSnackBarConfig) {
        let config: MdSnackBarConfig = {}
        if (selfConfig) {
            config = selfConfig;
        } else {
            config = {
                duration: 1500,
                extraClasses: ["snackbar-top"]
            }
        }
        let snackBarRef = snackBar.open(content, "", config);
        return snackBarRef;
    }
}

export interface IMessageBoxContainer {
    Title: string;
    Content: string;
    Visible: boolean;
    showOK: boolean;
    showCancel: boolean;
    showIgnore: boolean;
    onResult: (result: string) => void;

    GrowlMessage: Message[];
}

export enum MessageResult {
    Ok,
    Cancel,
    Ignore
}

export class GrowlMessageType {
    static Info: GrowlMessageType = new GrowlMessageType("info", 0);
    static Warning: GrowlMessageType = new GrowlMessageType("warn", 1);
    static Error: GrowlMessageType = new GrowlMessageType("error", 2);
    static Success: GrowlMessageType = new GrowlMessageType("success", 3);

    private name: string;
    private value: number;
    constructor(name: string, value: number) {
        this.name = name;
        this.value = value;
    }

    get Name(): string { return this.name; }
    get Value(): number { return this.value; }
}